package com.jihu.sharding.algorithm;

import org.apache.shardingsphere.api.sharding.hint.HintShardingAlgorithm;
import org.apache.shardingsphere.api.sharding.hint.HintShardingValue;

import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;

/**
 * 数据表hint分片算法类
 */
public class MyHintTableShardingAlgorithm implements HintShardingAlgorithm<Integer> {

    /**
     * 数据表hint分片算法类
     * @param availableTargetNames 所有真实数据表名称集合
     * @param shardingValue 数据表过滤条件
     * @return
     */
    @Override
    public Collection<String> doSharding(Collection<String> availableTargetNames, HintShardingValue<Integer> shardingValue) {
        // 拼装要查询的数据表名称(我们是想查询course_2表中的记录)
        String key = shardingValue.getLogicTableName() + "_" + shardingValue.getValues().toArray()[0];
        if(availableTargetNames.contains(key)){
            return Collections.singletonList(key);
        }
        throw new UnsupportedOperationException("route "+ key +" is not supported ,please check your config");
    }
}
